package com.xt.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xt.shop.Model.AdminInfo;
import com.xt.shop.Model.LoginRequest;
import com.xt.shop.mapper.AdminInfoMapper;
import com.xt.shop.service.AdminInfoService;
import com.xt.shop.util.PasswordEncoderUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author 24862
 * @description 针对表【admin_info】的数据库操作Service实现
 * @createDate 2024-01-08 16:00:03
 */
@Service
public class AdminInfoServiceImpl extends ServiceImpl<AdminInfoMapper, AdminInfo>
        implements AdminInfoService {
    @Resource
    AdminInfoMapper mapper;
    @Resource
    PasswordEncoderUtil passwordEncoderUtil;

    @Override
    public boolean Login(LoginRequest request) {
        AdminInfo selectOne = mapper.selectOne(new QueryWrapper<AdminInfo>()
                .eq("username", request.getUsername()));
        if (selectOne != null)
            return passwordEncoderUtil.check(request.getPassword(), selectOne.getPassword());
        return false;
    }

    @Override
    public int register(LoginRequest request) {
        if (mapper.selectOne(new QueryWrapper<AdminInfo>().eq("username",request.getUsername())) == null){
            AdminInfo info=new AdminInfo();
            String uuid = UUID.randomUUID().toString();
            info.setUsername(request.getUsername());
            info.setPassword(passwordEncoderUtil.encoder(request.getPassword()));
            info.setInputTime(new Date());
            info.setUpdateTime(new Date());
            info.setState(1);
            info.setUid(uuid);
            return mapper.insert(info);
        }
        return 0;
    }
}




